package cn.wolfcode.luowowo.listener;

import cn.wolfcode.luowowo.cache.service.IStrategyStatisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyStatisService strategyStatisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        // 获取所有攻略明细对象
        List<Strategy> list = strategyService.list();
        // 看看这个key 存不存在redis中
        for (Strategy strategy : list) {
            // 存在就跳过
            if (strategyStatisService.Extsis(strategy.getId()) == true) {
                continue;
            }
            // 不存在 new 一个Vo
            StrategyStatisVO vo = new StrategyStatisVO();
            // 把对象的属性值copy到Vo中
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
            // 存入redis 中
            strategyStatisService.setStrategyStatisVO(vo);
        }

        System.out.println("结束");




    }
}
